import React from 'react'
import {connect} from 'react-redux'
import {messages} from "share/common";
import { Button, message, Popover } from 'antd';
import SlideFrame from 'components/slide-frame';
import SearchArea from 'components/search-area';
import BusinessRule from 'containers/financial-account/interface-definition/business-rule';
import menuRoute from "routes/menuRoute";
import { setTypeInterface } from 'actions/financial-account';
import interfaceService from "containers/financial-account/interface-definition/interface-service";
import ResizeTable from 'components/resize-table'

class businessTypeDefinition extends React.Component{
  constructor(props){
    super(props);
    this.state = {
      searchForm: [
        {
          type: 'input',
          id: 'typeCode',
          label: messages('financial-account.key54')/*业务类型编码*/
        },{
          type: 'input',
          id: 'typeName',
          label: messages('financial-account.key55')/*业务类型名称*/
        }
      ],
      searchParams: {},
      columns: [
        {title: messages('financial-account.key52')/*核算业务类型编码*/,dataIndex: 'typeCode'},
        {title: messages('financial-account.key53')/*核算业务类型名称*/,dataIndex: 'typeName'},
        {title: messages('financial-account.key3')/*说明*/,dataIndex: 'comment', width: '30%', render: value => <Popover content={value} placement={'topLeft'}>{value}</Popover>},
        {title: messages('financial-account.key5')/*是否预置*/,dataIndex: 'isDefault', width: 96, render:(value) => value ? messages('common.yes')/*是*/ : messages('common.no')/*否*/},
        {title: messages('common.column.status')/*状态*/,dataIndex: 'isEnable', width: 80, render:(value) => value ? messages('common.valid')/*生效*/ : messages('financial-account.key6')/*不生效*/},
        {title: messages('common.operation')/*操作*/,dataIndex: 'operation', width: 110,render: (value,record) => {
            return <span>
              {
                !record.isDefault && (
                  <span>
                    <a onClick={e => this.handleEdit(e,record)}>{messages('common.edit')/*编辑*/}</a>
                    <span className="ant-divider"/>
                  </span>
                )
              }
              <a onClick={e => this.handleGoDetail(e,record)}>{messages('common.detail')/*详情*/}</a>
            </span>;}}
      ],
      data: [],
      loading: false,
      pagination: {
        total: 0
      },
      page: 0,
      showSlideFrame: false,
      title: '',
      editRecord: {},
      interfaceRecord: {},
      typeDefinitionDetail: menuRoute.getRouteItem('type-definition-detail')
    }
  }

  componentWillMount() {
    this.props.dispatch(setTypeInterface({}));
    this.setState({interfaceRecord: this.props.recordInfo},() => {
      this.getList();
    });
  }

  componentWillReceiveProps(nextProps) {
    this.setState({interfaceRecord: nextProps.recordInfo});
  }

  getList = () => {
    const { searchParams, page, interfaceRecord } = this.state;
    let temp = {
      functionId: interfaceRecord.id,
      typeCode: searchParams.typeCode,
      typeName: searchParams.typeName
    };
    Object.keys(temp).map(item => {
      !temp[item] && delete temp[item];
    });
    temp.page = page;
    this.setState({loading: true});
    interfaceService.getTypeList(temp).then(res => {
      this.setState({
        loading: false,
        data: res.data.rows,
        pagination: {
          total: Number(res.data.total),
          current: page + 1,
          onChange: this.onChangePager,
        }
      });
    }).catch(e => {
      message.error(e.response.data.message);
      this.setState({loading: false});
    })
  };

  onChangePager = (page) => {
    if (page - 1 !== this.state.page)
      this.setState({
        page: page - 1
      }, () => {
        this.getList();
      })
  };

  search = (result) => {
    const { searchParams } = this.state;
    searchParams.typeCode = result.typeCode;
    searchParams.typeName = result.typeName;
    this.setState({searchParams, page: 0},() => {
      this.getList();
    })
  };

  clear = () => {
    this.setState({searchParams: {}},() => {
      this.getList();
    })
  };

  handleEdit = (e,record) => {
    e.stopPropagation();
    this.setState({
      title: messages('financial-account.key56')/*编辑业务接口定义*/,
      editRecord: record
    },() => {
      this.setState({showSlideFrame: true});
    })
  };

  handleGoDetail = (e,record) => {
    e.stopPropagation();
    this.props.dispatch(setTypeInterface(record));
    this.context.router.push(this.state.typeDefinitionDetail.url.replace(':tab','business-type-definition'));
  };

  handleNew = () => {
    this.setState({
      title: messages('financial-account.key57')/*新建业务接口定义*/,
      editRecord: {}
    },() => {
      this.setState({showSlideFrame: true});
    })
  };

  isRefresh = (isRefresh) => {
    this.setState({showSlideFrame: false});
    isRefresh === true && this.getList();
  };

  render(){
    const { searchForm, columns, data, loading, pagination, showSlideFrame, editRecord, title, interfaceRecord } = this.state;
    return(
      <div>
        <SearchArea searchForm={searchForm}
                    submitHandle={this.search}
                    clearHandle={this.clear}/>
        <div style={{margin:'10px 0px'}}>
          <Button type="primary" onClick={this.handleNew} disabled>{messages('common.create')}</Button>
        </div>
        <ResizeTable
          columns={columns}
          dataSource={data}
          loading={loading}
          rowKey={'id'}
          pagination={pagination}
          bordered/>
        <SlideFrame show={showSlideFrame}
                    title={title}
                    params={{
                      showSlideFrame,
                      editRecord,
                      interfaceRecord
                    }}
                    content={BusinessRule}
                    onClose={(isRefresh) => this.isRefresh(isRefresh)}
                    afterClose={(isRefresh) => this.isRefresh(isRefresh)}/>
      </div>
    )
  }
}

function mapStateToProps(state){
  return {

  }
}

businessTypeDefinition.contextTypes = {
  router: React.PropTypes.object
};

export default connect(mapStateToProps)(businessTypeDefinition);
